package rentcar.repository.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import rentcar.domain.UserPrincipal;
import rentcar.repository.UserDao;
import rentcar.repository.base.AbstractHibernateDao;

@Repository
@Transactional
public class UserRepository extends AbstractHibernateDao<UserPrincipal, Long> implements UserDao {
	
	@Override
	public UserPrincipal findById(Long userId) {
		Criteria cr = getSession()
				.createCriteria(UserPrincipal.class, "users")
				.add(Restrictions.eq("userId", userId));
		return (UserPrincipal) cr.uniqueResult();
	}
	
	public UserPrincipal findByCredentials(String login, String password) {
		Criteria cr = getSession()
				.createCriteria(UserPrincipal.class, "users")
				.add(Restrictions.eq("login", login))
				.add(Restrictions.eq("password", password));
		return (UserPrincipal) cr.uniqueResult();
	}
}

